//
//  FreshFoodSecondLevelCell.swift
//  SmartHousekeeper
//
//  Created by 刘森 on 2020/2/21.
//  Copyright © 2020 LS. All rights reserved.
//

import UIKit


class FreshFoodSecondLevelCell: UITableViewCell {
    
    
    lazy private var foodImageView: UIImageView = {
        let value = UIImageView()
        return value
    }()
    
    lazy private var foodNameLabel: UILabel = {
        let value = UILabel(textColor: UIColor.black, textFont: KFont(Adapter(20)))
        return value
    }()
    lazy private var foodAreaLabel: UILabel = {
        let value = UILabel(textColor: KColorRGBA(129, 129, 129), textFont: KFont(Adapter(15)))
        return value
    }()
    lazy private var foodUnitLabel: UILabel = {
        let value = UILabel(textColor: KColorRGBA(129, 129, 129), textFont: KFont(Adapter(15)))
        return value
    }()
    lazy private var foodPriceLabel: UILabel = {
        let value = UILabel()
        return value
    }()
    lazy var addAndSubView = FreshFoodSecondLevelAddAndSubView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.addSubviews([foodImageView, foodNameLabel, foodAreaLabel, foodUnitLabel, foodPriceLabel, addAndSubView])
        self.selectionStyle = .none
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.foodImageView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(Adapter(10))
            make.width.height.equalTo(Adapter(100))
        }
        
        self.foodNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self.foodImageView)
            make.left.equalTo(self.foodImageView.snp.right).offset(Adapter(10))
            make.right.lessThanOrEqualToSuperview()
        }
        self.foodAreaLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self.foodNameLabel.snp.bottom).offset(Adapter(6))
            make.left.equalTo(self.foodImageView.snp.right).offset(Adapter(10))
            make.right.lessThanOrEqualToSuperview()
        }
        self.foodUnitLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self.foodAreaLabel.snp.bottom).offset(Adapter(6))
            make.left.equalTo(self.foodImageView.snp.right).offset(Adapter(10))
            make.right.lessThanOrEqualToSuperview()
        }
        self.foodPriceLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.foodImageView.snp.right).offset(Adapter(10))
            make.bottom.equalTo(self.foodImageView).offset(Adapter(6))
        }
        self.addAndSubView.snp.makeConstraints { (make) in
            make.height.equalTo(Adapter(30))
            make.centerY.equalTo(self.foodPriceLabel)
            make.right.equalToSuperview().offset(-Adapter(16))
        }
    }
    var model: FreshFoodSecondLevelCellModel? {
        didSet {
            self.foodImageView.image = ImageName(self.model?.imageName ?? "")
            self.foodNameLabel.text = self.model?.foodName
            self.foodAreaLabel.text = self.model?.foodArea
            self.foodUnitLabel.text = "单位： " + (self.model?.foodUnit ?? "")
            self.foodPriceLabel.attributedText = getPriceAttstr(value:  self.model?.foodPrice ?? "")
            
        }
    }
    
    func getPriceAttstr(value: String) -> NSAttributedString {
        
        let string = String(format: "￥%@", value)
        let attrStr = NSMutableAttributedString(string: string)
        let subTextRange = NSMakeRange(0, string.count)
        attrStr.addAttributes([NSAttributedString.Key.font: KFont(Adapter(14)), NSAttributedString.Key.foregroundColor:  KColorRGBA(58, 130, 232)], range: subTextRange)
        let range: Range = string.range(of: value)!
        let location: Int = string.distance(from: value.startIndex, to: range.lowerBound)
        let value1: NSValue = NSValue(range: NSMakeRange(location, value.count))
        attrStr.addAttributes([NSAttributedString.Key.foregroundColor: KColorRGBA(58, 130, 232), NSAttributedString.Key.font: KFontBold(Adapter(20))], range: value1.rangeValue)
        return attrStr
    }
    
    func restartCount(){
        self.addAndSubView.count = 0
    }
}


enum FreshFoodSecondLevelAddAndSubViewType {
    case normal
    case limitOne
}

class FreshFoodSecondLevelAddAndSubView: UIView {
    
    
    var type: FreshFoodSecondLevelAddAndSubViewType = .normal
    lazy private var addImageView: UIButton = {
        let value = UIButton(normalImage: ImageName("jiahao"))
        return value
    }()
    lazy private var subImageView: UIButton = {
        let value = UIButton(normalImage: ImageName("jianhao"))
        return value
    }()
    lazy private var countLabel: UILabel = {
        let value = UILabel(textColor: UIColor.black, textFont: KFont(Adapter(17)))
        return value
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubviews([addImageView, subImageView, countLabel])
        subImageView.isHidden = true
        
        addImageView.sce.add(.touchUpInside) { [weak self] (button) in
            self?.count += 1
        }
        subImageView.sce.add(.touchUpInside) { [weak self] (button) in
            self?.count -= 1
        }

    }
    
    override func layoutSubviews() {
        addImageView.snp.makeConstraints { (make) in
            make.right.centerY.equalToSuperview()
            make.width.height.equalTo(Adapter(30))
        }
        countLabel.snp.makeConstraints { (make) in
            make.left.equalTo(subImageView.snp.right).offset(Adapter(8))
            make.right.equalTo(addImageView.snp.left).offset(-Adapter(8))
            make.centerY.equalToSuperview()
        }
        subImageView.snp.makeConstraints { (make) in
            make.left.centerY.equalToSuperview()
            make.width.height.equalTo(Adapter(30))
        }
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var count: Int = 0 {
        didSet {
            if self.count <= 0 {
                countLabel.isHidden = true
                subImageView.isHidden = true
                return
            }
            countLabel.isHidden = false
            subImageView.isHidden = false
            countLabel.text = String(format: "%ld", self.count)
            
            if self.type == .limitOne && self.count == 1 {
                countLabel.textColor = UIColor.red
                subImageView.isEnabled = false
            } else {
                countLabel.textColor = UIColor.black
                subImageView.isEnabled = true
            }
        }
    }
    

}

